package com.ssy.lingxi.enhance.handler.jpa;

import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Projections;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import com.ssy.lingxi.common.response.PageData;
import com.ssy.lingxi.enhance.constant.PNOOuterStatusEnum;
import com.ssy.lingxi.enhance.constant.PNOProcessInnerStatusEnum;
import com.ssy.lingxi.enhance.constant.PNOSupplierInnerStatusEnum;
import com.ssy.lingxi.enhance.entity.QProduceNoticeOrderDO;
import com.ssy.lingxi.enhance.model.qo.ProduceNoticeOrderQO;
import com.ssy.lingxi.enhance.model.vo.ProduceNoticeOrderListVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * QueryDsl工具
 * @author fhj
 * @date 2021-07-15
 * @version 2.0.0
 * */
@Component
public class QueryDslUtil {

    @Resource
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 平台后台 - 获取生产通知单列表
     * @param qo 列表搜索参数对象
     * @param pno 查询表实体
     * @param predicates 查询条件构造
     * */
    public PageData<ProduceNoticeOrderListVO> getPlatformPNOList(ProduceNoticeOrderQO qo, QProduceNoticeOrderDO pno, BooleanBuilder predicates) {
        //搜索条件
        if(StringUtils.isNotBlank(qo.getNoticeNo())) {
            predicates.and(pno.noticeNo.like("%" + qo.getNoticeNo() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getSupplierName())) {
            predicates.and(pno.supplierName.like("%" + qo.getSupplierName() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getProcessName())) {
            predicates.and(pno.processName.like("%" + qo.getProcessName() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getSummary())) {
            predicates.and(pno.summary.like("%" + qo.getSummary() + "%"));
        }
        if(qo.getStartTime() != null) {
            predicates.and(pno.createTime.goe(qo.getStartTime()));
        }
        if(qo.getEndTime() != null) {
            predicates.and(pno.createTime.loe(qo.getEndTime()));
        }
        if(qo.getOuterStatus() != null) {
            predicates.and(pno.outerStatus.eq(qo.getOuterStatus()));
        }

        //查询
        JPAQuery<ProduceNoticeOrderListVO> query = jpaQueryFactory
                .select(Projections.bean(ProduceNoticeOrderListVO.class,
                        pno.id,
                        pno.noticeNo,
                        pno.summary,
                        pno.supplierMemberId,
                        pno.supplierName,
                        pno.processMemberId,
                        pno.processName,
                        pno.createTime,
                        pno.deliveryBatch,
                        pno.deliveryId,
                        pno.deliveryNo,
                        pno.storageId,
                        pno.storageNo,
                        pno.logisticsOrderId,
                        pno.logisticsOrderNo,
                        pno.status,
                        pno.supplierInnerStatus,
                        pno.processInnerStatus,
                        pno.outerStatus,
                        pno.deliveryType,
                        pno.receiverAddressId,
                        pno.receiveAddress,
                        pno.receiveUserName,
                        pno.receiveUserTel,
                        pno.deliveryAddressId,
                        pno.deliveryAddress,
                        pno.deliveryUserName,
                        pno.deliveryUserTel,
                        pno.outerTaskType
                ))
                .from(pno)
                .where(predicates);

        //总记录数
        long totalCount = query.fetchCount();

        //列表数据
        List<ProduceNoticeOrderListVO> list = query.orderBy(pno.createTime.desc())
                .offset((qo.getCurrent() - 1) * qo.getPageSize())
                .limit(qo.getPageSize())
                .fetch();

        //封装外部状态名称
        list = list.stream().peek(a -> a.setOuterStatusName(PNOOuterStatusEnum.getMessage(a.getOuterStatus()))).collect(Collectors.toList());

        return new PageData<>(totalCount, list);
    }

    /**
     * 供应商 - 获取生产通知单列表
     * @param qo 列表搜索参数对象
     * @param pno 查询表实体
     * @param predicates 查询条件构造
     * */
    public PageData<ProduceNoticeOrderListVO> getSupplierPNOList(ProduceNoticeOrderQO qo, QProduceNoticeOrderDO pno, BooleanBuilder predicates) {
        //搜索条件
        if(StringUtils.isNotBlank(qo.getNoticeNo())) {
            predicates.and(pno.noticeNo.like("%" + qo.getNoticeNo() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getProcessName())) {
            predicates.and(pno.processName.like("%" + qo.getProcessName() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getSummary())) {
            predicates.and(pno.summary.like("%" + qo.getSummary() + "%"));
        }
        if(qo.getStartTime() != null) {
            predicates.and(pno.createTime.goe(qo.getStartTime()));
        }
        if(qo.getEndTime() != null) {
            predicates.and(pno.createTime.loe(qo.getEndTime()));
        }
        if(qo.getInnerStatus() != null) {
            predicates.and(pno.supplierInnerStatus.eq(qo.getInnerStatus()));
        }
        if(qo.getOuterStatus() != null) {
            predicates.and(pno.outerStatus.eq(qo.getOuterStatus()));
        }

        //查询
        JPAQuery<ProduceNoticeOrderListVO> query = jpaQueryFactory
                .select(Projections.bean(ProduceNoticeOrderListVO.class,
                        pno.id,
                        pno.noticeNo,
                        pno.summary,
                        pno.supplierMemberId,
                        pno.supplierName,
                        pno.processMemberId,
                        pno.processName,
                        pno.createTime,
                        pno.deliveryBatch,
                        pno.deliveryId,
                        pno.deliveryNo,
                        pno.storageId,
                        pno.storageNo,
                        pno.logisticsOrderId,
                        pno.logisticsOrderNo,
                        pno.status,
                        pno.supplierInnerStatus,
                        pno.processInnerStatus,
                        pno.outerStatus,
                        pno.deliveryType,
                        pno.receiverAddressId,
                        pno.receiveAddress,
                        pno.receiveUserName,
                        pno.receiveUserTel,
                        pno.deliveryAddressId,
                        pno.deliveryAddress,
                        pno.deliveryUserName,
                        pno.deliveryUserTel,
                        pno.outerTaskType
                ))
                .from(pno)
                .where(predicates);

        //总记录数
        long totalCount = query.fetchCount();

        //列表数据
        List<ProduceNoticeOrderListVO> list = query.orderBy(pno.createTime.desc())
                .offset((qo.getCurrent() - 1) * qo.getPageSize())
                .limit(qo.getPageSize())
                .fetch();

        //封装外部状态名称, 供应商内部状态名称
        list.forEach(a -> {
            a.setOuterStatusName(PNOOuterStatusEnum.getMessage(a.getOuterStatus()));
            a.setInnerStatusName(PNOSupplierInnerStatusEnum.getMessage(a.getSupplierInnerStatus()));
        });

        return new PageData<>(totalCount, list);
    }

    /**
     * 加工企业 - 获取生产通知单列表
     * @param qo 列表搜索参数对象
     * @param pno 查询表实体
     * @param predicates 查询条件构造
     * */
    public PageData<ProduceNoticeOrderListVO> getProcessPNOList(ProduceNoticeOrderQO qo, QProduceNoticeOrderDO pno, BooleanBuilder predicates) {
        //搜索条件
        if(StringUtils.isNotBlank(qo.getNoticeNo())) {
            predicates.and(pno.noticeNo.like("%" + qo.getNoticeNo() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getSupplierName())) {
            predicates.and(pno.supplierName.like("%" + qo.getSupplierName() + "%"));
        }
        if(StringUtils.isNotBlank(qo.getSummary())) {
            predicates.and(pno.summary.like("%" + qo.getSummary() + "%"));
        }
        if(qo.getStartTime() != null) {
            predicates.and(pno.createTime.goe(qo.getStartTime()));
        }
        if(qo.getEndTime() != null) {
            predicates.and(pno.createTime.loe(qo.getEndTime()));
        }
        if(qo.getInnerStatus() != null) {
            predicates.and(pno.processInnerStatus.eq(qo.getInnerStatus()));
        }
        if(qo.getOuterStatus() != null) {
            predicates.and(pno.outerStatus.eq(qo.getOuterStatus()));
        }

        //查询
        JPAQuery<ProduceNoticeOrderListVO> query = jpaQueryFactory
                .select(Projections.bean(ProduceNoticeOrderListVO.class,
                        pno.id,
                        pno.noticeNo,
                        pno.summary,
                        pno.supplierMemberId,
                        pno.supplierName,
                        pno.processMemberId,
                        pno.processName,
                        pno.createTime,
                        pno.deliveryBatch,
                        pno.deliveryId,
                        pno.deliveryNo,
                        pno.storageId,
                        pno.storageNo,
                        pno.logisticsOrderId,
                        pno.logisticsOrderNo,
                        pno.status,
                        pno.supplierInnerStatus,
                        pno.processInnerStatus,
                        pno.outerStatus,
                        pno.deliveryType,
                        pno.receiverAddressId,
                        pno.receiveAddress,
                        pno.receiveUserName,
                        pno.receiveUserTel,
                        pno.deliveryAddressId,
                        pno.deliveryAddress,
                        pno.deliveryUserName,
                        pno.deliveryUserTel,
                        pno.outerTaskType
                ))
                .from(pno)
                .where(predicates);

        //总记录数
        long totalCount = query.fetchCount();

        //列表数据
        List<ProduceNoticeOrderListVO> list = query.orderBy(pno.createTime.desc())
                .offset((qo.getCurrent() - 1) * qo.getPageSize())
                .limit(qo.getPageSize())
                .fetch();

        //封装外部状态名称, 加工企业内部状态名称
        list.forEach(a -> {
            a.setOuterStatusName(PNOOuterStatusEnum.getMessage(a.getOuterStatus()));
            a.setInnerStatusName(PNOProcessInnerStatusEnum.getMessage(a.getProcessInnerStatus()));
        });

        return new PageData<>(totalCount, list);
    }
}
